﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DbHelper;
using Logs;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Test.TestDbHelper {
    class TestMySqlHelper {
        private static string strConn = "Server=localhost;Database=mmcica; User=root;Password=123456;";

        private static MySqlConnection conn;

        public TestMySqlHelper() {
            try {
                conn = new MySqlConnection(strConn);
            } catch(Exception ex) {
                Log.Error(ex.Message);
            }
        }

        public void OpenDB() {
            try {
                if(conn != null) {
                    conn.Open();
                } else {
                    conn = new MySqlConnection(strConn);
                }
                if(conn.State != ConnectionState.Open) {
                    Log.Info("DB Open Error !");
                }
            } catch(Exception ex) {
                Log.Error(ex.Message);
            }
        }

        public void CloseDB() {
            try {
                if(conn.State == ConnectionState.Open) {
                    conn.Close();
                }
            } catch(Exception ex) {
                Log.Error(ex.Message);
            }
        }

        public bool IsExistTable(string table) {
            bool TableExists = false;
            try {
                OpenDB();
                DataTable dt = conn.GetSchema("Tables");
                foreach(DataRow row in dt.Rows) {
                    if(row.ItemArray[2].ToString() == table) {
                        TableExists = true;
                        break;
                    }
                }
                CloseDB();
                return TableExists;
            } catch(Exception ex) {
                Log.Error(ex.Message);
                return false;
            }
        }

        public DataTable SelectToDataTable(string sql) {
            DataTable Dt = null;
            if(conn.State != ConnectionState.Open) {
                OpenDB();
            }
            try {
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                MySqlCommand command = new MySqlCommand(sql,conn);
                adapter.SelectCommand = command;
                Dt = new DataTable();
                adapter.Fill(Dt);
            } catch(Exception ex) {
                Log.Error(ex.Message);
            }
            return Dt;
        }

        public bool ExecuteSQLNonquery(string sql) {
            if(conn.State != ConnectionState.Open) {
                OpenDB();
            }
            MySqlCommand cmd = new MySqlCommand(sql,conn);
            try {
                return cmd.ExecuteNonQuery() > 0;
            } catch(Exception ex) {
                Log.Error(ex.Message);
                return false;
            }
        }

        public int GetDataCount(string sql) {
            int Count = 0;
            try {
                if(conn.State != ConnectionState.Open) {
                    OpenDB();
                }
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                MySqlCommand cmd = new MySqlCommand(sql,conn);
                adapter.SelectCommand = cmd;
                DataTable Dt = new DataTable();
                adapter.Fill(Dt);
                Count = Dt.Rows.Count;
            } catch(Exception ex) {
                Log.Error(ex.Message);
            }
            return Count;
        }


        public void Test() {
            if(IsExistTable("t_doctor")) {
                Console.WriteLine("存在");
            } else {
                Console.WriteLine("不存在");
            }


            //MySqlConnection conn = new MySqlConnection(strConn);
            //for(int i = 1;i < 10000;i++) {
            //    string sql = string.Format("insert into t_dgv(id,alarmtype,lev) values({0},'{1}','{2}');",i,"DataGridView分页测试","深圳市超视科技有限公司");
            //    MySqlTool.ExecuteNonQuery(conn,CommandType.Text,sql);
            //}

            conn.Close();
        }
    }
}
